﻿package {
	import flash.display.*;
	import flash.geom.*;
	public class covert {
		public static function cut(source,w:Number,h:Number,trans:Boolean = false):Array {
			var col:int = Math.ceil(source.width/w);
			var row:int = Math.ceil(source.height/h);
			var target:Array = [];
			var matrix = new Matrix();
			var rect:Rectangle = new Rectangle(0 , 0, w, h);			
			for (var j:int = 0; j < row; j++) {
				target.push([]);
				for (var i:int = 0; i < col; i++) {
					var bmp:BitmapData = new BitmapData(w,h,trans,0x00000000);
					matrix.tx = -i*w;
					matrix.ty = -j*h;
					bmp.draw(source,matrix,null,null,rect,true);
					target[j].push(bmp);
				}
			}
			return target;			
		}
		public static function divide(source,col:int,row:int,valid = null):Array {
			var w:Number = source.width/col;
			var h:Number = source.height/row;
			valid = valid == null ? col*row : valid;
			var target:Array = [];
			var matrix = new Matrix();
			var rect:Rectangle = new Rectangle(0 , 0, w, h);
			out:
			for (var j:int = 0; j < row; j++) {
				for (var i:int = 0; i < col; i++) {
					if (valid <= 0)break out;					
					var bmp:BitmapData = new BitmapData(w,h,true,0x00000000);
					matrix.tx = -i*w;
					matrix.ty = -j*h;
					bmp.draw(source,matrix,null,null,rect,true);
					target.push(bmp);
					valid--;
				}
			}
			return target;
		}
/*
		private static function incise(source,w:Number,h:Number,col:int,row:int,valid:int):Array {
			//var source:BitmapData = s is Bitmap ? s.bitmapData : s;
			var target:Array = [];
			var matrix = new Matrix();
			var rect:Rectangle = new Rectangle(0 , 0, w, h);
			out:
			for (var j:int = 0; j < row; j++) {
				for (var i:int = 0; i < col; i++) {
					if (valid <= 0)break out;					
					var bmp:BitmapData = new BitmapData(w,h,true,0x00000000);
					matrix.tx = -i*w;
					matrix.ty = -j*h;
					bmp.draw(source,matrix,null,null,rect,true);
					target.push(bmp);
					valid--;
				}
			}
			return target;
		}
		*/
		public static function snap(source,target,tx,ty,size:Number = 50) {
			var s:BitmapData
			var t:BitmapData
			s = source is Bitmap ? source.bitmapData : source;
			t = target is Bitmap ? target.bitmapData : target;			
			var flag=false;
			var dr:uint,dg:uint,db:uint;
			var sr:uint,sg:uint,sb:uint;
			var tr:uint,tg:uint,tb:uint;
			var dc:uint;
			var sc:uint,tc:uint;
			var color:Array = [];
			while (color.length < 20){
				var ii = int(Math.random() * source.width);
				var jj = int(Math.random() * source.height);
				sc = s.getPixel32(ii,jj);
				if((sc>>24) != 0){
					color.push({"c":sc&0x00FFFFFF,"x":ii,"y":jj});
				}
			}
			pass:for (var dy:int=- size; dy <= size; dy++) {
				for (var dx:int=- size; dx <= size; dx++) {
					for (var i:int=0; i < color.length; i++) {
						sc = color[i].c;
						tc = t.getPixel(color[i].x + dx + tx,color[i].y + dy + ty);
						sr=sc >> 16;
						sg=sc & 0x00FF00 >> 8;
						sb=sc & 0x0000FF;
						tr=tc >> 16;
						tg=tc & 0x00FF00 >> 8;
						tb=tc & 0x0000FF;
						dr=sr - tr;
						dg=sg - tg;
						db=sb - tb;
						dc=dr * dr + dg * dg + db * db;
						if (dc > 100) {
							break;
						}
					}
					if (i == color.length) {
						flag=true;
						break pass;
					}
				}
			}

			if (flag) {
				return {"dx":dx,"dy":dy}
			}
			return null
		}
		public static function transparent(source:BitmapData,arg1 = null,arg2 = null){
			var threshold:uint;
			if(arg1 == null){
				threshold = source.getPixel(0,0);
			}else {
				if(arg2 == null){
					threshold = arg1;
				}else{
					threshold = source.getPixel(arg1,arg2);
				}
			}
			var rect:Rectangle = new Rectangle(0,0,source.width,source.height);
			var origin:Point = new Point(0,0);
			var result = new BitmapData(source.width,source.height,true);
			result.copyPixels(source,rect,origin);
			result.threshold(source,rect,origin,"==",threshold,0x00000000,0x00E0E0E0,true);
			return result;
		}
		public static function assemble(arrange:Array){
			var map:Sprite = new Sprite();
			var colSpan:Number,rowSpan:Number = 0;
			for(var j:int = 0;j < arrange.length;j++){
				colSpan = 0;
				for(var i:int = 0;i < arrange[j].length;i++){
					var bmp:Bitmap = arrange[j][i];
					bmp.x = colSpan;
					bmp.y = rowSpan;
					map.addChild(bmp);
					colSpan += bmp.width;
				}
				rowSpan += bmp.height;
			}
			return map;
		}
	}
}